package service

import (
	"gin/common"
	"gin/model"
	"gin/response"
	"gin/validate"
)

func CreatePlay(Data validate.Play) (res model.Play, err error) {
	res.Name = Data.Name
	res.Description = Data.Description
	res.StoryId = Data.StoryId
	res.Sort = Data.Sort
	res.SceneId = Data.SceneId
	id, err := res.Insert()

	if err != nil {
		return res, err
	}

	return res.FindById(id)
}

func FindPlay(Param common.SelectQueryParam) (resData model.Play, err error) {
	var logModel model.Play
	resData, err = logModel.Find(Param)
	return
}

func SelectPlay(Param *common.SelectQueryParam) (total int64, resData []model.Play, err error) {
	var logModel model.Play

	total, resData, err = logModel.Select(Param)

	return
}

func UpdatePlay(Data validate.UpdatePlayWrapper) (resData model.Play, err error) {
	return resData.Update(model.Play{
		Id:          Data.Data.Id,
		Name:        Data.Data.Name,
		Description: Data.Data.Description,
		Sort:        Data.Data.Sort,
		SceneId:     Data.Data.SceneId,
		StoryId:     Data.Data.StoryId,
	})
}

func DeletePlay(param *common.Filter) {
	var logModel model.Play
	logModel.DeleteByFilter(param)
	return
}

func UpdatePlayVideoMediaId(mediaId uint64, playId uint64) error {
	_, err := model.Update(&model.Play{}, map[string]interface{}{
		"id": map[string]interface{}{
			"condition": "id = ?",
			"val":       playId,
		},
	},
		map[string]interface{}{
			"video_id": mediaId,
		})
	return err
}

func SelectPlayVideoList(limit int, offset int) (interface{}, interface{}, error) {
	var logModels []model.Play
	resp := response.SelectResp{
		Rows: &logModels,
	}

	err := model.Select(&model.Play{}, &resp, map[string]interface{}{
		"e": map[string]interface{}{
			"condition": " video_id > ?",
			"val":       0,
		},
	}, limit, offset, "id,name,video_id", "video_id DESC", []string{"VideoMedia"})

	return resp.Total, resp.Rows, err
}
